import requests
from bs4 import BeautifulSoup
from openpyxl import Workbook

t = []
n = []
b = []
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36",
"Cookie": "BIDUPSID=0F24FB149E086AC1577F145CD2283657; PSTM=1606195031; BAIDUID=0F24FB149E086AC103288BF8E74100D5:FG=1; BAIDUID_BFESS=0F24FB149E086AC103288BF8E74100D5:FG=1; BD_UPN=12314753; COOKIE_SESSION=15097934_1_1_1_0_0_0_0_1_0_0_0_0_0_0_2_0_1622514579_1622514577%7C2%230_1_1622514577%7C1; BD_HOME=1; H_PS_PSSID=34099_34004_33772_33855_33607_26350; delPer=0; BD_CK_SAM=1; PSINO=7; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_645EC=81e3KY94y3MIlN7swY9OnPXpbDX4xwiknn7QrQVNYkCyCAQeroLrWnc0j%2F4; BA_HECTOR=2425a1ak0hag2100uc1gbliso0r; BDSVRTM=0"
}
#爬取
url = "https://movie.douban.com/top250"
html = requests.get(url,headers = header)
#解析
soup = BeautifulSoup(html.text,"html.parser")
titles = soup.findAll(name="div",attrs={"class":"hd"})
nums = soup.findAll(name="div",attrs={"class":"pic"})
brief = soup.select(".bd")
for i in brief:
    b.append(i.p.contents[0].strip())
b.pop(0)
for i in nums:
    n.append(i.em.string)
for i in titles:
    t.append(i.span.string)
wb = Workbook()
#默认创建一个sheet
ws = wb.active
ws.title = "豆瓣top"
ws.append(["排名","名字","简介"])
for i,j,k in zip(n,t,b):
    ws.append([i,j,k])
wb.save("豆瓣排名top.xlsx")